package com.example.homelicaisystem.services.impl;

import com.example.homelicaisystem.dao.InvestmentMapper;
import com.example.homelicaisystem.entity.DTO.InvestmentDTO;
import com.example.homelicaisystem.entity.DTO.InvestmentDetailDTO;
import com.example.homelicaisystem.entity.Investment;
import com.example.homelicaisystem.entity.InvestmentDetail;
import com.example.homelicaisystem.entity.SelectResult;
import com.example.homelicaisystem.entity.exception.NoInvestmentException;
import com.example.homelicaisystem.entity.exception.WrongEnoughInvestment;
import com.example.homelicaisystem.services.InvestmentService;
import com.example.homelicaisystem.utils.UserUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InvestmentImpl implements InvestmentService {
    private final InvestmentMapper investmentMapper;

    public InvestmentImpl(InvestmentMapper investmentMapper) {
        this.investmentMapper = investmentMapper;
    }


    private static int BUY = 10;
    private static int SELL = 20;
    private static int CREATE = 0;
    @Override
    public Page<Investment> getList(InvestmentDTO data) {
        PageHelper.startPage(data.getPageNum(), data.getPageSize());
        return investmentMapper.getList(data);
    }

    @Override
    public Investment buy(InvestmentDetail data) {

        Investment investment = investmentMapper.selectInvestmentByName(data.getInvestmentName());
        //购买已录入的理财产品
        if (investment != null) {
            //买入的平均单价
            int unitPrice = data.getUnitPrice();
            //买入的数量
            int buyNum = data.getNumber();
            //持有的平均单价
            int averageCost = investment.getUnitPrice();
            //持有的数量
            int hasNum = investment.getNumber();
            //平均单价 = （持有的平均单价*持有数量 + 买入单价*买入数量） / 持有数量 + 买入数量
            averageCost = (averageCost * hasNum + unitPrice * buyNum) / (hasNum + buyNum);
            //修改平均单价
            investment.setUnitPrice(averageCost);
            //修改持有数量
            investment.setNumber(hasNum + buyNum);

            //investment数据库修改
            investmentMapper.update(investment);
        }
        //当第一次购买理财产品
        else {

            //investment数据库添加
            investmentMapper.insert(data);
            //获取新创建插入的investment对象
            investment = investmentMapper.selectInvestmentByName(data.getInvestmentName());
        }
        return investment;
    }

    @Override
    public Investment sell(InvestmentDetail data) {
        //查找对应的理财产品
        Investment investment = investmentMapper.selectInvestmentById(data.getInvestmentId());

        int number =investment.getNumber() - data.getNumber();
        if (number<0){
            throw new WrongEnoughInvestment("持有产品数量不足");
        }

        //修改持有数量
        investment.setNumber(number);

        if(investment.getNumber()==0){
            investmentMapper.delete(investment.getId());
        }else {
            investmentMapper.update(investment);
        }
        return investment;
    }

    @Override
    public List<SelectResult> selectInvestmentType() {
        return investmentMapper.selectInvestmentType();
    }

    @Override
    public void cancelBuy(InvestmentDetail data) {
        Investment investment = investmentMapper.selectInvestmentByName(data.getInvestmentName());

        //判断 购入前数量number是否为0
        int number = investment.getNumber() - data.getNumber();
        if (number == 0) {
            investmentMapper.delete(data.getInvestmentId());
        }
        if (number > 0) {
            //删除记录的单价 = （现在价值 - 记录花费）/ 购入前数量
            int nowMoney = investment.getNumber() * investment.getUnitPrice();
            int unitPrice = (nowMoney - data.getMoney()) / number;

            investment.setUnitPrice(unitPrice);
            investment.setNumber(number);
            investmentMapper.update(investment);
        }
    }

    @Override
    public List<SelectResult> selectInvestment(int investmentType) {
        return investmentMapper.selectInvestment(investmentType);
    }

    @Override
    public void cancelSell(InvestmentDetail data) {
        Investment investment = investmentMapper.selectInvestmentByName(data.getInvestmentName());

        //产品记录因为 数量清空而被删除了
        if (investment.getState()==999){
            //设置状态为创建状态
            investment.setState(CREATE);
        }else {
            int number = investment.getNumber() + data.getNumber();
            investment.setNumber(number);
            investmentMapper.update(investment);
        }
    }

}


